package com.gitee.fantaros.web.fan.netcut.views;

import com.gitee.fantaros.web.fan.netcut.bean.PasterInfo;
import com.gitee.fantaros.web.fan.netcut.service.PasterService;
import com.gitee.fantaros.web.fan.netcut.util.Str;
import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextArea;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.dom.Style;
import com.vaadin.flow.router.*;
import com.vaadin.flow.spring.annotation.UIScope;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 内容
 */
@Route("/content")
@PageTitle("剪切板内容")
@UIScope
public class ContentView extends VerticalLayout implements BeforeEnterObserver {
    private String name = null;
    private String encodedPassword = null;
    private PasterInfo pasterInfo;
    private final TextField head;
    private final TextArea content;
    private final Div msgLabel;

    private final PasterService pasterService;

    public ContentView(@Autowired PasterService pasterService) {
        this.pasterService = pasterService;
        head = new TextField();
        head.setTitle("剪切板名");
        head.setWidth("100%");
        head.setReadOnly(true);
        content = new TextArea();
        content.setTitle("剪切板内容");
        content.setWidth("100%");
        content.setHeight("500px");
        Button refreshButton = new Button();
        refreshButton.setText("刷新");
        refreshButton.addClickListener(this::onRefreshButtonClick);
        Button saveButton = new Button();
        saveButton.setText("保存");
        saveButton.addClickListener(this::onSaveButtonClick);
        Button exitButton = new Button();
        exitButton.setText("退出");
        exitButton.addClickListener(e -> this.getUI().ifPresent(ui -> ui.navigate("/")));
        add(head);
        add(content);
        HorizontalLayout horizontalLayout = new HorizontalLayout();
        horizontalLayout.setWidth("100%");
        horizontalLayout.add(refreshButton);
        horizontalLayout.add(saveButton);
        horizontalLayout.add(exitButton);
        add(horizontalLayout);
        msgLabel = new Div();
        add(msgLabel);
        setAlignItems(Alignment.CENTER);
    }

    public void onRefreshButtonClick(ClickEvent<Button> event) {
        refreshContent();
    }

    private void refreshContent() {
        Style style = msgLabel.getStyle();
        style.setColor("white");
        msgLabel.setText("");
        if (name != null && pasterService.checkEncodedPasswd(name, encodedPassword)) {
            pasterInfo = pasterService.queryInfoById(name);
            head.setValue(pasterInfo.getPasterName());
            if (pasterInfo.getContent() != null) {
                content.setValue(pasterInfo.getContent());
                content.focus();
            }
        }
    }

    public void onSaveButtonClick(ClickEvent<Button> event) {
        if (name != null) {
            pasterInfo.setModifyTime(Str.toString(new Date()));
            pasterInfo.setContent(this.content.getValue());
            pasterService.updateContent(pasterInfo);
            Style style = msgLabel.getStyle();
            style.setColor("green");
            msgLabel.setText("剪切板已保存");
        } else {
            Style style = msgLabel.getStyle();
            style.setColor("red");
            msgLabel.setText("没有指定剪切板名字");
        }
    }

    @Override
    public void beforeEnter(BeforeEnterEvent beforeEnterEvent) {
        QueryParameters queryParameters = beforeEnterEvent.getLocation().getQueryParameters();
        Map<String, List<String>> params = queryParameters.getParameters();
        if (params.get("name") != null && !params.get("name").isEmpty()) {
            name = params.get("name").get(0);
            encodedPassword = params.get("pwd").get(0);
            refreshContent();
        }
    }
}
